﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HZ.Supervise;
using HZ.Supervise.IBLL;
using HZ.Supervise.Model;
using HZ.Supervise.Model.Enum;
using System.Collections.Generic;

namespace HZ.Supervise.Web.Secondphase.ReportHospital
{
	public partial class HospitalScaleList :BasePage
	{
        public HospitalScaleList()
        {
            //设置页面 模块以及操作类型
            this.ModuleNo = Module.补助管理.ToString();
            this.PageOperateType = OperateType.查看.ToString();
        }

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				BindArea();
				banding();
                //banding1();
			}
			
		}
		/// <summary>
		/// 查询事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void btnQuery_Click(object sender, EventArgs e)
		{
            AspNetPager1.CurrentPageIndex = 1;
			banding();
		}
		/// <summary>
		/// 绑定事件
		/// </summary>
		private void banding()
		{
			int totalrecord = 0;
             string areaId="";
			HZ.Supervise.IBLL.IBLL_ProcureCatalog iprocureCatalog = HZ.Supervise.BLLFactory.IBLL_ProcureCatalog();
			string strwhere = "";
			if (!string.IsNullOrEmpty(txtProcureCatalogid.Text))//流水号
			{
				strwhere += "and ProcureCatalogID='"+txtProcureCatalogid.Text+"'";
			}
			if (!txtproductname.Text.Trim().Equals(""))//药品名称
			{
				strwhere +="and ProductName like '%"+txtproductname.Text+"%'";
			}
			if (!txtmedicineMoDelName.Text.Trim().Equals(""))//剂型
			{
				strwhere += "and MedicineModelName like '%" + txtmedicineMoDelName.Text + "%'";
			}
			if (!txtoutlookc.Text.Trim().Equals(""))// 规格
			{
				strwhere += "and Outlookc like '%" + txtoutlookc.Text + "%'";
			}
			if (!txtFactor.Text.Trim().Equals(""))//包装
			{
				strwhere += "and Factor like '%" + txtFactor.Text + "%'";
			}
            if (!string.IsNullOrEmpty(ddlAreaName0.SelectedValue))
            {
                if (ddlAreaName0.SelectedValue == "530101")
                {
                    areaId = "530100";
                }
                else
                {
                    areaId = ddlAreaName0.SelectedValue;
                }
            }
           
            strwhere += "and SortSource=" + ddltype.SelectedValue;
			//dt = new DataTable();
            //根据地区Id查询出该地区是否有已经有值
			 DataTable dt = iprocureCatalog.GetScaleInfo(strwhere,areaId,1, this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out totalrecord);
			AspNetPager1.RecordCount = totalrecord;
			gvwDistributionRatio.DataSource = dt;
			gvwDistributionRatio.DataBind();
		}
        ///// <summary>
        ///// 绑定事件
        ///// </summary>
        //private void banding1()
        //{
        //    int totalrecord = 0;
        //    string areaId = "";
        //    HZ.Supervise.IBLL.IBLL_ProcureCatalog iprocureCatalog = HZ.Supervise.BLLFactory.IBLL_ProcureCatalog();
        //    string strwhere = " ";
        //    if (!string.IsNullOrEmpty(txtProcureCatalogid0.Text))//流水号
        //    {
        //        strwhere += " and ProcureCatalogID='" + txtProcureCatalogid0.Text + "'";
        //    }
        //    if (!txtproductname0.Text.Trim().Equals(""))//药品名称
        //    {
        //        strwhere += " and ProductName like '%" + txtproductname0.Text + "%'";
        //    }
        //    if (!txtmedicineMoDelName0.Text.Trim().Equals(""))//剂型
        //    {
        //        strwhere += " and MedicineModelName like '%" + txtmedicineMoDelName0.Text + "%'";
        //    }
        //    if (!txtoutlookc0.Text.Trim().Equals(""))// 规格
        //    {
        //        strwhere += " and Outlookc like '%" + txtoutlookc0.Text + "%'";
        //    }
        //    if (!txtFactor0.Text.Trim().Equals(""))//包装
        //    {
        //        strwhere += " and Factor like '%" + txtFactor0.Text + "%'";
        //    }
        //    if (!string.IsNullOrEmpty(ddlAreaName1.SelectedValue))
        //    {
        //        if (ddlAreaName1.SelectedValue == "530101")
        //        {
        //            areaId = "530100";
        //        }
        //        else
        //        {
        //            areaId = ddlAreaName1.SelectedValue;
        //        }
        //    }
        //    strwhere += "and SortSource=" + ddltype.SelectedValue;
        //    //dt = new DataTable();
        //    DataTable dt = iprocureCatalog.GetScaleInfo(strwhere,areaId,0, this.AspNetPager2.CurrentPageIndex, this.AspNetPager2.PageSize, out totalrecord);
        //    AspNetPager2.RecordCount = totalrecord;
        //    GridviewFinish.DataSource = dt;
        //    GridviewFinish.DataBind();
        //}
		/// <summary>
		/// 绑定地区
		/// </summary>
		public void BindArea()
		{
			IBLL_Area ia = new HZ.Supervise.BLL.BLL_Area();
			IList<Area> lstArea = new List<Area>();
			string userAreaId = CurrentAccount.GetUser().Org.Area.AreaID;
			//判断用户所属地区，如果用户是省级的 则可以列出全部区域
				int i;
				lstArea = ia.GetSonAreas(userAreaId, 1, 0, out i);
				foreach (Area area in lstArea)
				{
					ListItem ddlitem = new ListItem();
					ddlitem.Text = area.AreaName;
					ddlitem.Value = area.AreaID;
                    ddlAreaName0.Items.Add(ddlitem);
                    //ddlAreaName1.Items.Add(ddlitem);
                    //DropDownList1.Items.Add(ddlitem);
				}

				ListItem b = new ListItem();
				b.Text = "--请选择--";
				b.Value = "";
                ddlAreaName0.Items.Insert(0,b);
                //ddlAreaName1.Items.Insert(0, b);
                //DropDownList1.Items.Insert(0, b);
                ddlAreaName0.SelectedIndex = 1;//CurrentAccount.GetUser().Org.Area.AreaID;
			//}

		}
        /// <summary>
        /// 批量设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnBatLock_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtRatio2.Text))
            {
                HZ.Web.WebUtil.Alert("请填写补助比例！", this);
                return;
            }
            double Ratio = Convert.ToDouble(txtRatio2.Text)/100;
            if ((Convert.ToDouble(Ratio)) > 1)
            {
                HZ.Web.WebUtil.Alert("补助比例不能大于1！", this);
                return;
            }
            string  strwhere = "and SortSource=" + ddltype.SelectedValue;
            HZ.Supervise.IBLL.IBLL_ProcureCatalog iprocureCatalog = HZ.Supervise.BLLFactory.IBLL_ProcureCatalog();
            if (RadioAll.Checked)
            {
                DistributionRatio distribution = new DistributionRatio();
                distribution.DistributionName = CurrentAccount.GetUser().UserID;
                string strArea = ddlAreaName0.SelectedValue;
                if (!strArea.Equals(""))
                {
                    if (strArea == "530101")
                    {
                        distribution.DAreaId = "530100";
                        distribution.DAreaName = ddlAreaName0.SelectedItem.Text;
                    }
                    else
                    {
                        distribution.DAreaId = ddlAreaName0.SelectedValue;
                        distribution.DAreaName = ddlAreaName0.SelectedItem.Text;
                    }
                }
                else 
                {
                    HZ.Web.WebUtil.Alert("请选择地区！", this);
                    return;
                }
                distribution.DistributionState = 0;
                distribution.DistributionTime = DateTime.Now;
                distribution.Ratio = Convert.ToDouble(Ratio);

                DistributionRatioLog distributionlog = new DistributionRatioLog();
                distributionlog.DistributionName = CurrentAccount.GetUser().UserID;
                distribution.DistributionState = 0;
                if (!strArea.Equals(""))
                {
                    if (strArea == "530101")
                    {
                        distribution.DAreaId = "530100";
                        distribution.DAreaName = ddlAreaName0.SelectedItem.Text;
                    }
                    else
                    {
                        distribution.DAreaId = ddlAreaName0.SelectedValue;
                        distribution.DAreaName = ddlAreaName0.SelectedItem.Text;
                    }
                }
                distributionlog.DistributionTime = DateTime.Now;
                distributionlog.Ratio = Convert.ToDouble(Ratio);
               int i= iprocureCatalog.InsertIntoDistributionRatioAll(distribution, distributionlog, strwhere,0);
               if (i > 0)
               {
                   HZ.Web.WebUtil.Alert("设置比例成功！", this);
               }
               else 
               {
                   PageError("设置比例失败","");
               }
                this.banding();
            }
            else 
            {
                bool checke1 = false;
                IList<DistributionRatio> distributionRationList = new List<DistributionRatio>();
                IList<DistributionRatioLog> distributionRationLogList = new List<DistributionRatioLog>();
                for (int i = 0; i < gvwDistributionRatio.Rows.Count; i++)
                {
                    CheckBox check = (CheckBox)this.gvwDistributionRatio.Rows[i].FindControl("chk");
                    if (check.Checked)
                    {
                     string guid= Guid.NewGuid().ToString();
                        //获取当前的数据判断是否为空，若不为空则更新，若为空则为添加
                        Label lblRation = ((Label)this.gvwDistributionRatio.Rows[i].FindControl("Label1"));
                        string stRation = lblRation.Text;
                        DistributionRatio distribution = new DistributionRatio();
                        distribution.DProcureCatalogid = gvwDistributionRatio.Rows[i].Cells[1].Text;
                        distribution.DistributionName = CurrentAccount.GetUser().UserID;
                        distribution.DistributionRatioId = guid;
                        string strArea = ddlAreaName0.SelectedValue;
                        if (!strArea.Equals(""))
                        {
                            if (strArea == "530101")
                            {
                                distribution.DAreaId = "530100";
                                distribution.DAreaName = ddlAreaName0.SelectedItem.Text;
                            }
                            else 
                            {
                                distribution.DAreaId = strArea;
                                distribution.DAreaName = ddlAreaName0.SelectedItem.Text;
                            }
                           
                        }
                        else
                        {
                            HZ.Web.WebUtil.Alert("请选择地区！", this);
                            return;
                        }
                            distribution.DistributionState = 0;
                      
                        distribution.DistributionTime = DateTime.Now;
                        distribution.Ratio = Convert.ToDouble(Ratio);
                        Label lblDistributionRatioId = ((Label)this.gvwDistributionRatio.Rows[i].FindControl("lblDistributionRatioId"));
                        //判断补助Id是否为空，若为空则新增，若不为空则更新
                        distributionRationList.Add(distribution);

                        DistributionRatioLog distributionlog = new DistributionRatioLog();
                        distributionlog.DProcureCatalogid = gvwDistributionRatio.Rows[i].Cells[1].Text;
                        distributionlog.DistributionName = CurrentAccount.GetUser().UserID;
                        distributionlog.DistributionRatioId = guid;
                        distribution.DistributionState = 0;
                        if (!strArea.Equals(""))
                        {
                            distributionlog.DAreaId = ddlAreaName0.SelectedValue;
                            distributionlog.DAreaName = ddlAreaName0.SelectedItem.Text;
                        }
                        distributionlog.DistributionTime = DateTime.Now;
                        distributionlog.Ratio = Convert.ToDouble(Ratio);
                        distributionRationLogList.Add(distributionlog);
                        checke1 = true;
                    }
                }
                if (!checke1)
                {
                    HZ.Web.WebUtil.Alert("请选择要设置比例的数据！", this);
                    return;
                }
                int res = iprocureCatalog.InsertIntoDistributionRatioList(distributionRationList, distributionRationLogList);
                if (res == 1)
                {
                    this.banding();
                    HZ.Web.WebUtil.Alert("设置比例成功！", this);
                    Label3.Text = "";
                    //this.banding1();
                }
                else
                {
                    PageError("批量更新失败！", "");
                }
                RadioOne.Checked = true;
            }
           
            
            //根据选择的数量来批量更新
           
        }
		//取消编辑
		protected void gvwDistributionRatio_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
		{
			this.gvwDistributionRatio.EditIndex = -1;
			this.banding();
		}

		protected void gvwDistributionRatio_RowEditing(object sender, GridViewEditEventArgs e)
		{
			this.gvwDistributionRatio.EditIndex = e.NewEditIndex;
			this.banding();
		}

		protected void gvwDistributionRatio_RowUpdating(object sender, GridViewUpdateEventArgs e)
		{
			if (Page.IsValid)
			{
				int rowIndex = e.RowIndex;
				string userId = this.gvwDistributionRatio.DataKeys[rowIndex].Value.ToString();
				TextBox txtRation = (TextBox)this.gvwDistributionRatio.Rows[rowIndex].FindControl("txtRatio");
				double strRation =Convert.ToDouble(txtRation.Text.Trim())/100;
				if (string.IsNullOrEmpty(strRation.ToString()))
				{
					HZ.Web.WebUtil.Alert("比例不能为空！", this);
					((TextBox)gvwDistributionRatio.Rows[rowIndex].FindControl("txtRatio")).Focus();
					return;
				}
				if (Convert.ToDouble(strRation)>1)
				{
						HZ.Web.WebUtil.Alert(" 比例不能大于1！", this);
					((TextBox)gvwDistributionRatio.Rows[rowIndex].FindControl("txtRatio")).Focus();
					return;
				}
				string guid= Guid.NewGuid().ToString();
				HZ.Supervise.IBLL.IBLL_ProcureCatalog iprocureCatalog = HZ.Supervise.BLLFactory.IBLL_ProcureCatalog();
				Label lblRation = ((Label)this.gvwDistributionRatio.Rows[rowIndex].FindControl("Label1"));
				string stRation = lblRation.Text;
				DistributionRatio distribution = new DistributionRatio();
				distribution.DProcureCatalogid= gvwDistributionRatio.Rows[rowIndex].Cells[1].Text;
				distribution.DistributionName = CurrentAccount.GetUser().UserID;
				
				string strArea = ddlAreaName0.SelectedValue;
                if (!strArea.Equals(""))
                {
                    if (strArea == "530101")
                    {
                        distribution.DAreaId = "530100";
                    }
                    else 
                    {
                        distribution.DAreaId = strArea;
                    }
                    
                    distribution.DAreaName = ddlAreaName0.SelectedItem.Text;
                }
                else 
                {
                    HZ.Web.WebUtil.Alert("请选择地区！", this);
                    return;
                }
				distribution.DistributionState = 0;
				distribution.DistributionTime = DateTime.Now;
				distribution.Ratio =Convert.ToDouble(strRation);
				DistributionRatioLog distributionlog = new DistributionRatioLog();
				distributionlog.DProcureCatalogid = gvwDistributionRatio.Rows[rowIndex].Cells[1].Text;
				distributionlog.DistributionName = CurrentAccount.GetUser().UserID;
                distributionlog.DAreaId = ddlAreaName0.SelectedValue;
                distributionlog.DAreaName = ddlAreaName0.SelectedItem.Text;
				distributionlog.DistributionState = 0;
				distributionlog.DistributionTime = DateTime.Now;
				distributionlog.Ratio = Convert.ToDouble(strRation);
				//若不为空则更新
				int res = 0;
				if (!string.IsNullOrEmpty(stRation))
				{
					Label lblDistributionRatioId = ((Label)this.gvwDistributionRatio.Rows[rowIndex].FindControl("lblDistributionRatioId"));
					distribution.DistributionRatioId = lblDistributionRatioId.Text;
					distributionlog.DistributionRatioId = guid;
					res=iprocureCatalog.UpdateIntoDistributionRatio(distribution, distributionlog);
				}
				//若为空则添加
				else 
				{
					distributionlog.DistributionRatioId = guid;
					distribution.DistributionRatioId = guid;
					res=iprocureCatalog.InsertIntoDistributionRatio(distribution, distributionlog);
				}
				if (res > 0)
				{
					this.gvwDistributionRatio.EditIndex = -1;
					this.banding();
                    //this.banding1();
                    HZ.Web.WebUtil.Alert("设置比例成功！", this);
				}
				else 
				{
					PageError("更新失败！", "");
				}
			}
		}
		/// <summary>
		/// 高级查询
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void lbnSwitch_Click(object sender, EventArgs e)
		{
			if (lbnSwitch.CommandArgument == "simple")
			{
				ltlAdvance.Text = "";
				lbnSwitch.CommandArgument = "advance";
				lbnSwitch.Text = "简单查询";
			}
			else
			{
				ltlAdvance.Text = "<tbody id='advance' style='display:none'>";
				lbnSwitch.CommandArgument = "simple";
				lbnSwitch.Text = "高级查询";

				//清空数据
				txtProcureCatalogid.Text = "";
				txtproductname.Text = "";
				txtmedicineMoDelName.Text = "";
				ddlAreaName0.SelectedIndex = -1;
				txtoutlookc.Text = "";
				txtFactor.Text = "";
			}
		}
		/// <summary>
		/// 分页
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
		{
			banding();
		}
		/// <summary>
		/// 单条设置比例
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void btnBatResetPws_Click(object sender, EventArgs e)
		{ 

		}

        protected void HzLinkButton1_Click(object sender, EventArgs e)
        {
            //if (HzLinkButton1.CommandArgument == "simple")
            //{
            //   // Literal2.Text = "";
            //    HzLinkButton1.CommandArgument = "advance";
            //    HzLinkButton1.Text = "单条设置";
            //}
            //else
            //{
            //    //Literal2.Text = "<tbody id='advance' style='display:none'>";
            //    HzLinkButton1.CommandArgument = "simple";
            //    HzLinkButton1.Text = "批量设置";

            //    //清空数据
            //    ddlAreaName.SelectedIndex = -1;
            //    Radioyes.Checked = true;
            //    txtRatio2.Text = "";
            //}
        }

        protected void txtRatio2_TextChanged(object sender, EventArgs e)
        {
            if (txtRatio2.Text.Length>0)
            {
                RadioMore.Checked = true;
            }
        }

        protected void ddlAreaName_SelectedIndexChanged(object sender, EventArgs e)
        {
            RadioOne.Checked = true;
        }
       

	}
}
